<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" media="all" th:href="@{/admin/layui/css/layui.css}">
    <link rel="stylesheet" media="all" th:href="@{/admin/style/admin.css}">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <!-- 搜索 -->
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="请输入名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <select name="status">
                            <option value=""></option>
                            <option value="Y">启用</option>
                            <option value="N">禁用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn" data-type="search" lay-submit lay-filter="LAY-search">搜索
                    </button>
                </div>
            </div>
        </div>

        <!-- 操作 -->
        <div class="layui-card-body">
            <div style="padding-bottom: 20px;">
                <button class="layui-btn layuiadmin-btn" data-type="add">添加</button>
                <button class="layui-btn layuiadmin-btn" data-type="reload">重载</button>
            </div>
            <table id="lay-list" lay-filter="lay-list"></table>
            <script type="text/html" id="table-operation">
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="run"><i
                        class="layui-icon layui-icon-refresh-1"></i>运行</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>

    <script type="text/html" id="switchTpl">
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="LAY-status" {{ d.status === 'Y' ? 'checked' : '' }}>
    </script>
</div>

<script th:src="@{/admin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '/admin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'admin'], function () {
        var $ = layui.$, form = layui.form, table = layui.table, admin = layui.admin;

        //监听搜索
        form.on('submit(LAY-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('lay-list', {
                where: field
            });
        });

        //列表管理
        table.render({
            elem: '#lay-list'
            , url: 'query'
            , method: 'post'
            , cols: [
                [
                    {type: 'numbers', width: 80, align: 'center', title: ''}
                    , {field: 'name', width: 300, align: 'center', title: '名称'}
                    , {field: 'cron', width: 200, align: 'center', title: 'cron表达式'}
                    , {field: 'methods', align: 'center', title: '方法'}
                    , {field: 'status', width: 200, align: 'center', title: '状态', templet: '#switchTpl', unresize: true}
                    , {title: '操作', width: 250, minWidth: 50, align: 'center', fixed: 'right', toolbar: '#table-operation'}
                ]
            ]
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , page: true
            , limit: 10
            , limits: [10, 15, 20, 25, 30]
            , text: {
                none: '暂无相关数据'
            }
            , done: function (res) {
                layer.closeAll('loading');
                admin.exit(res);
            }
        });

        form.on('switch(LAY-status)', function(obj){
            //执行 Ajax 后重载
            admin.req({
                url: 'changeStatus'
                , type: 'post'
                , data: {"id" : obj.value, "status": obj.elem.checked ? "Y" : "N"}
                , done: function (res) {
                    layer.msg(res.msg, {icon: 1});
                }
            });
        });

        //监听工具条
        table.on('tool(lay-list)', function (obj) {
            var data = obj.data;
            // 修改
            if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '编辑'
                    , content: 'toEdit?id=' + data.id
                    , area: ['500px', '500px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find("#LAY-submit");
                        //监听提交
                        iframeWindow.layui.form.on('submit(LAY-submit)', function (data) {
                            //执行 Ajax 后重载
                            admin.req({
                                url: 'edit'
                                , type: 'post'
                                , data: data.field
                                , done: function (res) {
                                    layer.close(index);
                                    //数据刷新
                                    table.reload('lay-list');
                                    layer.msg(res.msg, {icon: 1});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })
            }
            // 删除
            else if (obj.event === 'delete') {
                var field = {id: data.id}
                layer.confirm('确定删除？', {icon: 3, title: '确认删除'}, function (index) {
                    admin.req({
                        url: 'delete'
                        , data: field
                        , done: function (res) {
                            layer.close(index);
                            //数据刷新
                            table.reload('lay-list');
                            layer.msg(res.msg, {icon: 1});
                        }
                    });
                });
            }
            // 删除
            else if (obj.event === 'run') {
                var field = {id: data.id}
                layer.confirm('确定运行一次？', {icon: 3, title: '确认运行一次'}, function (index) {
                    admin.req({
                        url: 'run'
                        , data: field
                        , done: function (res) {
                            layer.close(index);
                            layer.msg(res.msg, {icon: 1});
                        }
                    });
                });
            }
        });

        // 点击事件触发
        $('.layui-btn.layuiadmin-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //事件
        var active = {
            // 新增
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加'
                    , content: 'toAdd'
                    , area: ['500px', '500px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find("#LAY-submit");
                        //监听提交
                        iframeWindow.layui.form.on('submit(LAY-submit)', function (data) {
                            //执行 Ajax 后重载
                            admin.req({
                                url: 'add'
                                , type: 'post'
                                , data: data.field
                                , done: function (res) {
                                    layer.close(index);
                                    //数据刷新
                                    table.reload('lay-list');
                                    layer.msg(res.msg, {icon: 1});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            },
            // 重载
            reload: function () {
                layer.confirm('确定重载吗？', {icon: 3, title: '重载'}, function (index) {
                    admin.req({
                        url: 'reload'
                        , done: function (res) {
                            layer.close(index);
                            layer.msg(res.msg, {icon: 1});
                        }
                    });
                });
            }
        }
    });
</script>
</body>
</html>
